package org.erp.common.page;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.List;
import java.util.function.Function;

/**
 * 表格分页数据对象
 *
 * @author black
 */

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class DataPageResp<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     * 总记录数
     */
    private long total;

    /**
     * 列表数据
     */
    private List<T> rows;

    public static <T> DataPageResp<T> of(PageQueryModel<T> pageQueryModel) {
        return new DataPageResp<>(pageQueryModel.getTotal(), pageQueryModel.getDataList());
    }

    /**
     * 构建分页查询模型对象
     * @param mapper 分页对象
     * @return 分页模型对象
     */
    public <R> DataPageResp<R> cover(Function<T,R> mapper) {
        return new DataPageResp<>(
                this.getTotal(),
                this.rows.stream()
                        .map(mapper)
                        .toList()
        );
    }

    public static <T> DataPageResp<T> of(Long total, List<T> rows) {
        return new DataPageResp<>(total,rows);
    }
}
